/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.zt.dangyuan;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.MenuItem;


import io.zt.dangyuan.customView.MyNavTab;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 */
public class ActivityItem2 extends BaseActivity {
    ViewPager viewPager;
    MyNavTab tabLayout;
    private String[] mTitles;

    private Adapter adapter11;
    private int pos;

    private int indexPostion = 0;
    private int index;

    private int type;//设定详细页面类型

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addCenterView(R.layout.main_item2);
        toolbar.setNavigationIcon(R.drawable.icon_select_all_light);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        tabLayout = (MyNavTab) findViewById(R.id.tabs);
        Intent intent = getIntent();
        pos = intent.getIntExtra("pos", 0);
        index = intent.getIntExtra("index", -1);
        cid = intent.getStringExtra("cid");
        mTitles = intent.getStringArrayExtra("titles");
        switch (pos) {
            case 0:
                setTitleImg(R.drawable.title_qingnian);
                type = BBSDetailActivity.TYPE_COMMENT;
                break;
            case 1:
                setTitleImg(R.drawable.title_huodong);
                type = BBSDetailActivity.TYPE_BAOMING;
                break;

            case 4:
                setTitleImg(R.drawable.title_shenghuo);
                type = BBSDetailActivity.TYPE_COMMENT;
                break;
            case 5:
                setTitleImg(R.drawable.title_tuanqing);
                type = BBSDetailActivity.TYPE_COMMENT;
                cid = "ttyw";
                break;
            case 6:
                setTitleImg(R.drawable.title_dangjian);
                type = BBSDetailActivity.TYPE_COMMENT;
                break;
            case 7:
                setTitleImg(R.drawable.title_gongzuo);
                type = BBSDetailActivity.TYPE_COMMENT;
                break;
        }
        initDates();
        initEvents();
    }

    private void initDates() {
        if (viewPager != null) {
            setupViewPager(viewPager);
        }
        tabLayout.setTitles(mTitles, viewPager);
    }

    private void setupViewPager(ViewPager viewPager) {
        adapter11 = new Adapter(getSupportFragmentManager());

        if (pos == 5) {
            for (int i = 0; i < mTitles.length; i++) {
                MyFragment1 myFragment0 = MyFragment1.newInstance(type);
                myFragment0.setPos(BBSDetailActivity.TYPE_COMMENT);
                if(i==0&&index==-1){
                    myFragment0.setCid("ttyw");
                }else if(i==index){
                    myFragment0.setCid(cid);
                }
                adapter11.addFragment(myFragment0, "");
            }
        } else if (pos == 1) {
            for (int i = 0; i < mTitles.length; i++) {
                MyFragment1 myFragment0 = MyFragment1.newInstance(type);
                myFragment0.setPos(type);
                if(i==0&&index==-1){
                    myFragment0.setCid("pxhd");
                }else if(i==index){
                    myFragment0.setCid(cid);
                }
                adapter11.addFragment(myFragment0, "");
            }
        } else if (pos == 0) {
            for (int i = 0; i < mTitles.length; i++) {
                MyFragment1 myFragment0 = MyFragment1.newInstance(type);
                myFragment0.setPos(type);
                if(i==0&&index==-1){
                    myFragment0.setCid("xljkcp");
                }else if(i==index){
                    myFragment0.setCid(cid);
                }
                adapter11.addFragment(myFragment0, "");
            }
        }


        viewPager.setAdapter(adapter11);
        if (index != -1) {
            viewPager.setCurrentItem(index);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                startActivity(new Intent(this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
                finish();
                return true;

            case R.id.action_search:

            {
                Intent intent = new Intent(ActivityItem2.this, SearchActivity.class);


                if (pos == 5) {
                    switch (indexPostion) {
                        case 0:
                            cid = "ttyw";
                            break;
                        case 1:
                            cid = "txyw";
                            break;
                        case 2:
                            cid = "qnzs";
                            break;
                    }
                } else if (pos == 1) {
                    switch (indexPostion) {
                        case 0:
                            cid = "pxhd";
                            break;
                        case 1:
                            cid = "hdfb";
                            break;
                        case 2:
                            cid = "hdhg";
                            break;
                    }
                } else if (pos == 0) {
                    switch (indexPostion) {
                        case 0:
                            cid = "xljkcp";
                            break;
                        case 1:
                            cid = "xljkfd";
                            break;

                    }
                }
                intent.putExtra("type", type);
                intent.putExtra("cid", cid);
                startActivity(intent);
                return true;
            }


        }
        return super.onOptionsItemSelected(item);
    }

    private void initEvents() {
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                indexPostion = position;
                if (pos == 5) {
                    switch (position) {
                        case 0:
                            cid = "ttyw";
                            break;
                        case 1:
                            cid = "txyw";
                            break;
                        case 2:
                            cid = "qnzs";
                            break;
                    }
                    MyFragment1 myFragment0 = (MyFragment1) adapter11.getItem(position);
                    myFragment0.setCid(cid);
                    myFragment0.webRequst(true);
                } else if (pos == 1) {
                    switch (position) {
                        case 0:
                            cid = "pxhd";
                            break;
                        case 1:
                            cid = "hdfb";
                            break;
                        case 2:
                            cid = "hdhg";
                            break;
                    }
                    MyFragment1 myFragment0 = (MyFragment1) adapter11.getItem(position);
                    myFragment0.setCid(cid);
                    myFragment0.webRequst(true);
                } else if (pos == 0) {
                    switch (position) {
                        case 0:
                            cid = "xljkcp";
                            break;
                        case 1:
                            cid = "xljkfd";
                            break;

                    }
                    MyFragment1 myFragment0 = (MyFragment1) adapter11.getItem(position);
                    myFragment0.setCid(cid);
                    myFragment0.webRequst(true);
                }
            }

            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPixels) {
                tabLayout.scroll(position, positionOffset);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    static class Adapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragments = new ArrayList<>();
        private final List<String> mFragmentTitles = new ArrayList<>();

        public Adapter(FragmentManager fm) {
            super(fm);
        }

        public void addFragment(Fragment fragment, String title) {
            mFragments.add(fragment);
            mFragmentTitles.add(title);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitles.get(position);
        }
    }
}
